using System.Collections.Generic;
using UnityEngine;

public enum ESolarName
{
    None,
    Sun,
    Mercury,
    Venus,
    Earth,
    Mars,
    Jupiter,
    Saturn,
    Uranus,
    Neptune,
}

public class StarManager : MonoBehaviour
{
    public static StarManager m_Intance;
    public Transform m_eye;
    public Transform m_originPosition;
    public Transform m_target_look;

    public float m_moveSpeed = 2f;
    public ESolarName m_curSolarName;

    private Transform _move_target;
    private Dictionary<ESolarName, StarListItem> _dicStar;

    private bool m_isEyeMoving = false;

    private void Awake()
    {
        m_Intance = this;
        _dicStar = new Dictionary<ESolarName, StarListItem>();
        m_curSolarName = ESolarName.None;
        _move_target = m_originPosition;
        
        var item = new StarListItem();
        item.m_solar_name = m_curSolarName;
        item.m_look_point = m_originPosition;
        item.m_target_star = m_target_look;
        RegisterStarItem(m_curSolarName, item);
        m_eye.LookAt(m_target_look);
        
    }
    
    //相机移动逻辑
    private void FixedUpdate()
    {
        if (!m_isEyeMoving) return;
        
        m_eye.localPosition = Vector3.Lerp(m_eye.localPosition, Vector3.zero, Time.deltaTime * m_moveSpeed);
        m_eye.LookAt(m_target_look);

        if (Vector3.Distance(m_eye.position, _move_target.position) < 0.3f)
        {
            SetMoveState(false);
        }
    }

    public void RegisterStarItem(ESolarName pName, StarListItem pItem)
    {
        if (!_dicStar.ContainsKey(pName))
        {
            _dicStar[pName] = pItem;
        }
        else
            Debug.Log($"m_dicStar字典已经包含{pName}");
    }

    public void SetTargetBySolarName(ESolarName pName)
    {
        if (_dicStar.TryGetValue(pName, out var starItem))
        {
            m_curSolarName = starItem.m_solar_name;
            m_target_look = starItem.m_target_star;
            _move_target = starItem.m_look_point;
            m_eye.SetParent(_move_target);
            SetMoveState(true);
        }
        else
            Debug.Log($"{pName}没有在m_dicStar字典中找到对应的数据");
    }

    private void SetMoveState(bool pMove)
    {
        m_isEyeMoving = pMove;
    }

    public bool GetValueEyeMoving()
    {
        return m_isEyeMoving;
    }

}
